package rjgc.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import rjgc.mall.common.MessageConstant;
import rjgc.mall.common.MySessionContext;
import rjgc.mall.common.Result;
import rjgc.mall.entity.Goods;
import rjgc.mall.entity.ShoppingCart;
import rjgc.mall.entity.User;
import rjgc.mall.entity.vo.ShoppingCarList;
import rjgc.mall.entity.vo.UserHistoryOrders;
import rjgc.mall.service.ShoppingCartService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ShoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingcartService;

    //添加至购物车
    @RequestMapping ("/AddToShoppingCart")
    public Integer addToShoppingCart(@RequestParam Integer userid,@RequestBody Goods good) {return shoppingcartService.AddToShoppingCart(userid,good);}

    //从购物车中删除
    @DeleteMapping("/RemoveFromShoppingCart")
    public Integer removefromShoppingCart(@RequestParam Integer id){return shoppingcartService.RemoveFromShoppingCart(id);}

    //根据选中状态进行商品移除
    @DeleteMapping("/RemoveFromShoppingCartByStatus")
    public Integer removefromShoppingCartByStatus(@RequestParam Integer userid){return shoppingcartService.RemoveFromShoppingCartByStatus(userid);}

    //查看用户购物车商品
//    @RequestMapping("/showUserShoppingCart")
//    public List<ShoppingCart> showUserShoppingCart(@RequestParam Integer userid){
//        return shoppingcartService.ShowUserShoppingCart(userid);
//    };

    @RequestMapping("/showUserShoppingCart")
    public Result showUserShoppingCart(@RequestHeader("userToken") String userToken){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        ShoppingCarList result = shoppingcartService.viewList(user.getId());
        return new Result(true, MessageConstant.USER_GET_SHOPPINGCAR,result);
    }



    //对购物车商品进行全选
    @RequestMapping("/SelectAll")
    public Integer selectAll(@RequestParam Integer userid){
        return shoppingcartService.SelectAll(userid);
    }

    //对购物车商品进行全不选
    @RequestMapping("/NotSelectAll")
    public Integer notSelectAll(@RequestParam Integer userid){
        return shoppingcartService.NotSelectAll(userid);
    }

    //对单个购物车商品进行选取状态的更改
    @RequestMapping("/SelectOneOrNot")
    public Integer selectOneOrNot(@RequestParam Integer id){
        return shoppingcartService.SelectOneOrNot(id);
    }
    //购买勾选的商品
    @RequestMapping("/BuySelectedGoods")
    public Integer buySelectedGoods(@RequestHeader("usertoken") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        User user=(User) session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy/MM/dd");
        System.out.println(dateFormat.format(date));
        String tradetime=String.valueOf(dateFormat.format(date));
        return shoppingcartService.BuySelectedGoods(user.getId(), String.valueOf(tradetime), user.getDefaultTraderPlace());
    }
    //将选中的商品移入收藏
    @RequestMapping("/MoveToCollection")
    public Integer moveToCollection(@RequestParam Integer userid){return shoppingcartService.MoveToCollection(userid); }

    //设置购买数量
    @RequestMapping("/setBuyNum")
    public Integer setBuyNum(@RequestParam Integer id,@RequestParam Integer userid,@RequestParam Integer num) {
        return shoppingcartService.SetBuyNum(id,userid,num);
    }

    //新的购买勾选的商品
    @RequestMapping("/BuySelectedGoods2")
    public Integer buySelectedGoods2(@RequestHeader("usertoken") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        User user=(User) session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy/MM/dd");
        System.out.println(dateFormat.format(date));
        String tradetime=String.valueOf(dateFormat.format(date));
        return shoppingcartService.BuySelectedGoods2(user.getId(), String.valueOf(tradetime), user.getDefaultTraderPlace());
    }
    //新的购买勾选的商品
    /*@RequestMapping("/BuySelectedGoods2")
    public Integer buySelectedGoods2(@RequestParam int userid,@RequestParam String tradeplace){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy/MM/dd");
        System.out.println(dateFormat.format(date));
        String tradetime=String.valueOf(dateFormat.format(date));
        return shoppingcartService.BuySelectedGoods2(userid, String.valueOf(tradetime), tradeplace);
    }*/
}
